#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @auth:  spacelacc
# @date:  2025/04/14
# @func:  custom widget for file system operations
# @vers:  V1


import os
from PySide6.QtCore    import QDir, Signal
from PySide6.QtWidgets import QWidget, QTreeView
from PySide6.QtWidgets import QFileSystemModel, QPushButton
from PySide6.QtWidgets import QDialog, QHBoxLayout, QVBoxLayout
from PySide6.QtWidgets import QStackedWidget


class CustomFileBrowser(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.system = QFileSystemModel()
        self.system.setFilter(QDir.AllDirs|QDir.NoDotAndDotDot|QDir.Files)
        self.rootpath = os.path.join(QDir.currentPath(), 'ultrasonic_probe')
#       self.system.setRootPath(QDir.currentPath())
        self.system.setRootPath(self.rootpath)

        self.browser = QTreeView()
        self.browser.setModel(self.system)
        self.browser.setSelectionMode(QTreeView.ExtendedSelection)
        self.browser.setSelectionBehavior(QTreeView.SelectRows)
#       self.browser.setRootIndex(self.system.index(QDir.currentPath()))
        self.browser.setRootIndex(self.system.index(self.rootpath))
        self.browser.header().setVisible(False)
        self.browser.setColumnHidden(1, True)
        self.browser.setColumnHidden(2, True)
        self.browser.setColumnHidden(3, True)

        # executing buttons
        self.goup = QPushButton('go up')
        self.godp = QPushButton('go deep')
#       self.copy = QPushButton('copy')
#       self.paste = QPushButton('paste')
        self.rename = QPushButton('rename')
        self.delete = QPushButton('delete')
        self.createfile = QPushButton('create file')
        self.createdir = QPushButton('create directory')
#       ctbuttons = CustomCompactHBox()
        ctbuttons = QHBoxLayout()
        ctbuttons.setSpacing(0)
        ctbuttons.setContentsMargins(0, 0, 0, 0)
        ctbuttons.addWidget(self.goup)
        ctbuttons.addWidget(self.godp)
#       ctbuttons.addWidget(self.copy)
#       ctbuttons.addWidget(self.paste)
        ctbuttons.addWidget(self.rename)
        ctbuttons.addWidget(self.delete)
        ctbuttons.addWidget(self.createdir)
        ctbuttons.addWidget(self.createfile)

        # set all up
#       ctcontainer = CustomCompactVBox(self)
        ctcontainer = QVBoxLayout(self)
        ctcontainer.setSpacing(0)
        ctcontainer.setContentsMargins(0, 0, 0, 0)
        ctcontainer.addLayout(ctbuttons)
        ctcontainer.addWidget(self.browser)

        # connecting signals
        self.goup.clicked.connect(self.goUp)
        self.godp.clicked.connect(self.goDown)
#       self.browser.clicked.connect(self.)

    def goUp(self):
        current = self.system.rootDirectory()
        current.cdUp()
        self.system.setRootPath(current.absolutePath())
        self.browser.setRootIndex(self.system.index(current.absolutePath()))

    def goDown(self):
        selected = self.browser.selectedIndexes()
        if not selected:
            return

        current = self.system.filePath(selected[0])
        self.system.setRootPath(current)
        self.browser.setRootIndex(self.system.index(current))

    def getSelected(self):
        itemsChosen = []
        selected = self.browser.selectedIndexes()
        for index in selected:
            itemsChosen.append(self.system.filePath(index))
        return itemsChosen

